package com.meshnetics.gb.type.impl.custom;

/**
 * Collection type. Doesn't hold any additional info. Any data types are accepted as values of collection.
 *
 * @author AKovylin
 */
public abstract class CollectionDataType extends CustomDataType {
    protected CollectionDataType(String name) {
        this(new String[]{name});
    }

    protected CollectionDataType(String[] aliases) {
        super(aliases);
    }

    protected String formatShortLogString(CustomDataValue value) {
        if (!(value instanceof CollectionDataValue)) {
            throw new IllegalArgumentException("not a collection");
        }
        CollectionDataValue coll = (CollectionDataValue) value;
        StringBuffer sb = new StringBuffer();
        sb.append(value.getType().getName()).append('(');
        sb.append("size=").append(coll.getCollectionSize()).append(',');
        sb.append('[');
        for (int i = 0; i < coll.getCollectionSize(); i++) {
            if (i > 0) {
                sb.append(',');
            }
            sb.append(coll.getElement(i).getType().getMandatoryFormatter().formatShortLogString(coll.getElement(i)));
        }
        sb.append(']').append(')');
        return sb.toString();
    }

    protected String formatMaxLogString(CustomDataValue value) {
        if (!(value instanceof CollectionDataValue)) {
            throw new IllegalArgumentException("not a collection");
        }
        CollectionDataValue coll = (CollectionDataValue) value;
        StringBuffer sb = new StringBuffer();
        sb.append(value.getType().getName()).append('(').append(value.getType().getClass().getName()).append(',');
        sb.append("size=").append(coll.getCollectionSize()).append(',');
        sb.append('[');
        for (int i = 0; i < coll.getCollectionSize(); i++) {
            if (i > 0) {
                sb.append(',');
            }
            sb.append(coll.getElement(i).getType().getMandatoryFormatter().formatMaxLogString(coll.getElement(i)));
        }
        sb.append(']').append(')');
        return sb.toString();
    }
}
